package com.quanyan.club.web;

import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.vo.req.ReqClubAnnouncement;
import com.quanyan.club.entity.vo.req.ReqClubAnnouncementPage;
import com.quanyan.club.service.ClubAnnouncementService;
import com.quanyan.common.Plugin.PageObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;


/**
 * 俱乐部公告管理接口
 * Created by zhang on 16/5/30.
 */
@RestController
@RequestMapping("/api/club/announcement")
public class ClubAnnouncementController extends BaseController {
    @Autowired
    private ClubAnnouncementService clubAnnouncementService;

    /**
     * 俱乐部公告列表
     * @param reqClubAnnouncementPage
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public APIResponse<PageObj> list(@Valid @RequestBody ReqClubAnnouncementPage reqClubAnnouncementPage,HttpServletRequest request){
        if(null == reqClubAnnouncementPage.getParamMap().get("clubId")){
            return APIResponse.returnFail("PARAMETER_CANT_BE_EMPTY");
        }
        Integer uid = getUid(request, true);
        PageObj pageObj = clubAnnouncementService.selectClubAnnouncementList(reqClubAnnouncementPage,uid);

        return APIResponse.returnSuccess(pageObj);
    }

    /**
     * 添加俱乐部公告
     * @param reqClubAnnouncement
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public APIResponse add(@Valid @RequestBody ReqClubAnnouncement reqClubAnnouncement,HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        int n = clubAnnouncementService.addClubAnnouncement(reqClubAnnouncement,uid);
        if(n>0){
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("俱乐部公告添加失败，请重新添加");
        }
    }

    /**
     * 删除俱乐部公告
     * @param map
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public APIResponse delete(@Valid @RequestBody Map map,HttpServletRequest request){
        int uid = (int)request.getAttribute("uid");
        if(null == map.get("announcementId")){
            return APIResponse.returnFail("参数不能为空");
        }
        try{
            int announcementId = (int)map.get("announcementId");
            int n = clubAnnouncementService.deleteClubAnnouncement(announcementId,uid);
            if(n>0){
                return APIResponse.returnSuccess();
            }else if(n == -1){
                return APIResponse.returnFail("您没有删除该公告的权限");
            }else if(n == -2){
                return APIResponse.returnFail("该公告不存在");
            }else{
                return APIResponse.returnFail("俱乐部公告删除失败，请重新删除");
            }
        }catch (Exception e){
            e.printStackTrace();
            return APIResponse.returnFail("参数必须为正整数");
        }

    }

}
